//
//  XGTableView.m
//  XGSeeDetails
//
//  Created by 小果 on 16/8/29.
//  Copyright © 2016年 小果. All rights reserved.
//

#import "XGTableView.h"
#import "XGCellModel.h"
#import "XGCellFrame.h"
#import "XGMessageCell.h"

@interface XGTableView()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *messageData;
@property (nonatomic, weak) UIView *bottomV;
@property (nonatomic, assign) CGRect rect;
@end
@implementation XGTableView
-(NSMutableArray *)messageData{
    if (nil == _messageData) {
        _messageData = [XGCellFrame cellSourceWithName:@"MessageSource"];
    }
    return _messageData;
}
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self = [super initWithFrame:frame style:style]) {
        self.dataSource = self;
        self.delegate = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = [UIColor magentaColor];
    }
    return self;
}
#pragma mark - tableView的数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.messageData.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *messageID = @"messageCell";
    XGMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:messageID];
    if (nil == cell) {
        cell = [[XGMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageID];
    }
    
    XGCellFrame *cFrame = self.messageData[indexPath.row];
    
    cell.cellFrame = cFrame;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 取出对应位置的模型
    XGCellFrame *cellF = self.messageData[indexPath.row];
    
    XGCellModel *cellM = cellF.cellModel;
    
    if (cellF.isOpened) {
        //  改变开关状态 开 --> 关
        cellF.opened = NO;
    }else
    {//  改变开关状态 关 --> 开
        cellF.opened = YES;
    }
    // 注意:每次点击需要重新设置frame 并刷新表格数据
    [cellF setCellModel:cellM];
    
//    [self reloadData];
    [self reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
    // 获取当前选中Cell的最大Y值
//    CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];
//    CGRect rect = [tableView convertRect:rectInTableView toView:[tableView superview]];
  
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    XGCellFrame *cellF = self.messageData[indexPath.row];
    return cellF.cellHeight;
}

#pragma mark - 滑动Cell时的操作
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 删除按钮
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        // 从模型数组中将选中的Cell的索引移除
        [self.messageData removeObjectAtIndex:indexPath.row];
        // 从table中删除对应索引的行
        [self deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        // 刷新表格
        [self reloadData];
        
    }];
    action1.backgroundColor = [UIColor redColor];
    
    // 已读按钮
    UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"标为已读" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        
    }];
    action2.backgroundColor = [UIColor colorWithRed:255/255.0 green:194/255.0 blue:68/255.0 alpha:1.0];
    
    // 置顶按钮
    UITableViewRowAction *action3 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        XGCellFrame *cellF = [[XGCellFrame alloc] init];
        // 1、取出要置顶行对应的模型数据
        cellF = self.messageData[indexPath.row];
        // 2、将选中的行从数组中移除
        [self.messageData removeObjectAtIndex:indexPath.row];
        // 3、将置顶行的模型数据插入到第一行
        [self.messageData insertObject:cellF atIndex:0];
        
        [self reloadData];
        
    }];
    action3.backgroundColor = [UIColor lightGrayColor];
    
    return @[action1,action2,action3];
}

#pragma mark - Cell出现时或者刷新的动画
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
        //缩放
        cell.layer.transform = CATransform3DMakeScale(0.6, 0.6, 1);
        [UIView animateWithDuration:1 animations:^{
            cell.layer.transform = CATransform3DMakeScale(1, 1, 1);
        }];

}

@end
